/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.raster.grid;

import java.awt.Image;
import java.io.IOException;
import java.lang.ref.SoftReference;

import chunmap.data.feature.Raster;

/**
 * @author chunquedong
 * 
 */
public class TileImpl extends Raster {
	private int x;
	private int y;
	private Grid parent;
	private SoftReference<Object> data;

	public String getName() {
		String name = x + "_" + y;
		return name;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Grid getParent() {
		return parent;
	}

	public void setParent(Grid parent) {
		this.parent = parent;
	}

	public Image getImage() {
		if (data == null)
			return loadData();

		Image obj = (Image)data.get();
		if (obj == null)
			return loadData();

		return obj;
	}

	public void setImage(Object data) {
		this.data = new SoftReference<Object>(data);
	}

	public Image loadData() {
		try {
			Image obj = parent.getGridData().getData(parent.getName(),
					this.getName());
			setImage(obj);
			return obj;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}